package com.jindianjichi.auth.appliccation.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jindianjichi.auth.appliccation.convert.AuthRolePermissionDTOConvert;
import com.jindianjichi.auth.appliccation.dto.AuthRolePermissionDTO;

import com.jindianjichi.auth.domain.BO.AuthRolePermissionBO;
import com.jindianjichi.auth.domain.service.AuthRolePermissionDomainService;
import com.jindianjichi.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rolePermission")
@Slf4j
public class AuthRolePermissionController {
    @Autowired
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增用户权限
     * @param authRolePermissionDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色id不能为空");
            Preconditions.checkArgument(authRolePermissionDTO.getPermissionIdList().size()>0, "权限列表不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConvert.instance.convertToBoAuthRolePermission(authRolePermissionDTO);
            return Result.ok(authRolePermissionDomainService.add(authRolePermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色权限失败");
        }
    }



}
